import Vue from 'vue'
import App from './App'
import store from './store' // store
import plugins from './plugins' // plugins
import './permission' // permission
import uView from "uview-ui";
import request from './utils/request.js'
// import Player from "xgplayer";
// import "xgplayer/dist/index.min.css";
import config from '@/config'
import SnowflakeId from 'snowflake-id';
import { toast } from './utils/common'

// H5端路由拦截
//#ifdef H5
// 被拦截的API路径列表
const blockedApiPaths = ['/api/actuator'];

// 检查URL是否包含被拦截的路径
function checkBlockedUrl(url) {
  return blockedApiPaths.some(blockedPath => url.includes(blockedPath));
}

// 监听路由变化
function setupRouteInterceptor() {
  // 监听 popstate 事件（浏览器前进后退）
  window.addEventListener('popstate', function(event) {
    const currentUrl = location.href;
    if (checkBlockedUrl(currentUrl)) {
      console.log("拦截浏览器导航到被禁止的API路径:", currentUrl);
      uni.reLaunch({
        url: '/pages/common/404/index'
      });
    }
  });

  // 监听 hashchange 事件（hash路由变化）
  window.addEventListener('hashchange', function(event) {
    const currentUrl = location.href;
    if (checkBlockedUrl(currentUrl)) {
      console.log("拦截hash路由变化到被禁止的API路径:", currentUrl);
      uni.reLaunch({
        url: '/pages/common/404/index'
      });
    }
  });
}

// 在Vue实例创建前设置路由拦截
setupRouteInterceptor();

// 立即检查当前URL并持续监听
function checkAndRedirect() {
  const currentUrl = location.href;
  if (checkBlockedUrl(currentUrl)) {
    console.log("检测到被禁止的API路径:", currentUrl);
    // 立即替换当前历史记录，避免用户通过后退按钮回到被禁止的页面
    history.replaceState(null, '', '/pages/common/404/index');
    // 跳转到404页面
    if (typeof uni !== 'undefined') {
      uni.reLaunch({
        url: '/pages/common/404/index'
      });
    } else {
      // 如果uni还未初始化，则直接修改location
      location.hash = '/pages/common/404/index';
    }
    return true;
  }
  return false;
}

// 应用启动时立即检查
checkAndRedirect();

// 定期检查URL变化（作为备用方案）
setInterval(() => {
  checkAndRedirect();
}, 500);
//#endif
Vue.use(plugins)
Vue.use(uView);

Vue.config.productionTip = false
Vue.prototype.$store = store
// Vue.prototype.$Player = Player
Vue.prototype.$toast = toast
Vue.prototype.$request = request
Vue.prototype.$config = config


uni.$config = config
uni.$toast = toast
// 雪花id
uni.$guid = num => {
  const id = new SnowflakeId();
  return id.generate();
};

App.mpType = 'app'

const app = new Vue({
  ...App
})

app.$mount()
